package com.tlcb.dcdp.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.opensymphony.xwork2.ActionSupport;
import com.tlcb.dcdp.bean.User;
import com.tlcb.dcdp.service.UserService;

@Controller
public class UserController
{

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init(HttpServletRequest request, HttpServletResponse response, Model model)
	{
		System.out.println("init");
		return "init";
	}
	
	
	//登陆
	@RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, @RequestParam(defaultValue="/")String returnUrl, String password,
        HttpServletRequest request, HttpServletResponse response, Model model)
	{
		User user = new User();
		user.setUserNm(username);
		user.setUserPwd(password);
		
		if(userService.checkUser(user))
		{
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			
			return "index";
		}
		else
		{
			request.setAttribute("error","用户名或密码错误" );
			return "login";
		}
	}
	
	//注册用户
	public String addUser(String username, @RequestParam(defaultValue="/")String returnUrl, String password,
	        HttpServletRequest request, HttpServletResponse response, Model model)
	{
		User user = new User();
		user.setUserNm(username);
		user.setUserPwd(password);
		
		userService.addUser(user);
		
		return "success";
	}
	
}
